package controllers.operset;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Oplog;
import rbac.models.User;
import utils.Md5Util;

import models.Cjdata;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;
import com.et.mvc.JspView;
import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;

public class ModifypwdController extends ApplicationController {

	public void index() throws Exception {
		User user = (User) rbac.getCurrentUser();
		String enpwd = Md5Util.encodeMD5String(user.getPassword());
		request.setAttribute("myenpwd", enpwd);	
	}

	
	public View savePwd(String oldpwd,String newpwd,String confirmpwd,String identifycode) throws Exception{
		User user = (User) rbac.getCurrentUser();
		Map<String,Object> result = new HashMap<String,Object>();
		
		String identifycodesession = (String) request.getSession()
				.getAttribute("identifycode");
		// 如果获得的验证码为null，说明验证码过期，必须刷新客户端重新获取
		if (identifycodesession == null) {
			result.put("success", false);
			result.put("msg", "验证码过期，请刷新验证码！");		
			return new JsonView(result);
		}
		// 输入的验证码不匹配
		if (!identifycode.equalsIgnoreCase(identifycodesession)) {
			result.put("success", false);
			result.put("msg", "验证码不正确！");	
			return new JsonView(result);
		}
		
		if (user.getPassword().equals(oldpwd))
		{
			if (newpwd.equals(confirmpwd))
			{
				user.setPassword(newpwd);
				user.save();
				Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")修改密码成功。");									
					
				result.put("success", true);
				result.put("msg", "密码修改成功。");
			}else{
				result.put("success", false);
				result.put("msg", "新密码与确认密码不一致，请重新输入。");				
			}
		}else{
			result.put("success", false);
			result.put("msg", "旧密码不正确，请重新输入。");	
		}
		return new JsonView(result);
	}
	
	
}